package com.qqtop10.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Category;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.BuildHtmlService;
import com.qqtop10.manager.service.CategoryService;
import com.qqtop10.manager.service.OneCategoryIconService;

@Controller
@RequestMapping("/manager/icon/")
public class CategoryIconController extends BaseController {
	@Autowired
	@Qualifier("oneCategoryIconService")
	private OneCategoryIconService oneCategoryIconService;

	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	@Qualifier("buildHtmlService")
	private BuildHtmlService buildService;

	// 根据一级分类的ID获取对应的icon
	@ResponseBody
	@RequestMapping("selectIcon.do")
	public Result selectIcon(Integer categoryId) {
		return ok("操作成功", oneCategoryIconService.selectIconByCategoryId(categoryId));
	}

	// 获取未删除的一级分类
	@ResponseBody
	@RequestMapping("getOneCategory.do")
	public Result getOneCategory() {
		List<Category> categories = categoryService.selectOneCategory();
		return ok("操作成功", categories);
	}

	@ResponseBody
	@RequestMapping("updateCategoryIcon.do")
	public Result updateCategoryIcon(HttpServletRequest request) {
		String icon = request.getParameter("icon");
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<>();
		map.put("icon", icon);
		map.put("id", id);
		int ret = categoryService.updateCategoryIcon(map);
		if (ret > 0) {
			return buildService.buildClassMenu();
//			return ok("操作成功");
		}
		return err("操作失败");
	}
}
